package edu.mobbuzz.page.menu;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import edu.mobbuzz.page.menu.outbox.OutboxPage;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.theme.Theme;
import java.io.IOException;

public class OutboxMenu extends Menu {
    private Form outboxForm;
    private Container outboxPanel; 
    
    private Label menuTitle;
    private Label menuIconLabel;
    private Label menuDescription;
    private Image menuIcon;   
    
    public OutboxMenu(){
        outboxForm = new Form(){
            public void keyPressed(int keyCode) {
                gameActionPerformed(keyCode);
            }        
        };             
        outboxPanel = new Container();
        
        menuTitle = new Label(getMenuTitle());
        menuDescription = new Label();
        try {
            menuIcon = Image.createImage("/icons/outbox.gif"); //Theme.getResources().getImage("outbox.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        menuIconLabel = new Label(menuIcon); 
    }
    public void showPage(){ 
        menuIconLabel.setAlignment(Component.CENTER);
        menuIconLabel.getStyle().setMargin(50, 0, 0, 0);
        menuIconLabel.getStyle().setBgTransparency(0);  
        
        menuTitle.setAlignment(Component.LEFT);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setFgColor(0xF59FBC);
        menuTitle.getStyle().setBgTransparency(0);   
         
        menuDescription.setText("Your shared RSS");      
        menuDescription.setAlignment(Component.LEFT);
        menuDescription.getStyle().setFont(Theme.getFontDescriptionStyle("SansSerif"));
        menuDescription.getStyle().setFgColor(0xFFFFFF);
        menuDescription.getStyle().setMargin(0, 0, 5, 0);         
        menuDescription.getStyle().setBgTransparency(0); 
        
               
        outboxPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        outboxPanel.addComponent(menuIconLabel);
        outboxPanel.addComponent(menuTitle);        
        outboxPanel.addComponent(menuDescription);
        
        outboxForm.setLayout(new BorderLayout()); 
        outboxForm.addComponent(BorderLayout.CENTER, outboxPanel);        
        outboxForm.addCommand(getExitCmd());
        outboxForm.addCommand(getOnlineCmd());
        outboxForm.addCommand(getSignOutCmd());
        outboxForm.addCommand(new Command("Open") {
            public void actionPerformed(ActionEvent evt) {
               openOutbox();
            }
        }); 
                 
        outboxForm.setCommandListener(getActionlistener());
        outboxForm.show();
    }

    public String getMenuTitle() {
        return MenuString.OUTBOX;
    }

    
    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);
        if(game == Display.GAME_FIRE){
            openOutbox();
        }else if(game == Display.GAME_RIGHT){
            getOutboxTransitionRight();
        }else if(game == Display.GAME_LEFT){
            getOutboxTransitionLeft();
        }
    }
    
    public void getOutboxTransitionRight(){
        outboxForm.setTransitionOutAnimator(Theme.getTransitionOut());
        CategoryMenu categoryMenu = new CategoryMenu();
        categoryMenu.setExitCmd(getExitCmd());
        categoryMenu.setActionlistener(getActionlistener());
        categoryMenu.setMessaging(getMessaging());
        categoryMenu.showPage();
    }
    
    public void getOutboxTransitionLeft(){
       outboxForm.setTransitionOutAnimator(Theme.getTransitionIn());
       InboxMenu inboxMenu = new InboxMenu();
       inboxMenu.setExitCmd(getExitCmd());
       inboxMenu.setActionlistener(getActionlistener());
       inboxMenu.setMessaging(getMessaging());
       inboxMenu.showPage();
    }

    public void openOutbox() {
        OutboxPage outboxPage = new OutboxPage();
        outboxPage.setExitCmd(getExitCmd());
        outboxPage.setActionlistener(getActionlistener());
        outboxPage.setMessaging(getMessaging());
        outboxPage.showPage();
    }
}
